package com.atguigu.juc;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

//1.创建一个callable实现类
class MyselfCallable implements Callable<Integer> {
        private int sum=0;
    @Override
    public Integer call() throws Exception {
        //2.实现call方法    将此进程执行的方法这在call方法中
        for (int i = 0; i <=100 ; i++) {
            sum+=i;
        }
        System.out.println(Thread.currentThread().getName());
        return sum;
    }
}

public class MyselfCallableDemo {
    public static void main(String[] args) {
        //创建callable接口的实现类
        MyselfCallable myselfCallable = new MyselfCallable();
        //把此接口的实现类当做对象传递到futureTask构造器中
        FutureTask futureTask = new FutureTask<>(myselfCallable);

        new Thread(futureTask,"线程名称1").start();


        try {
            //futureTask.get()可以返回方法的返回值
            System.out.println(futureTask.get());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }
}
